package thread;

import java.util.concurrent.ConcurrentHashMap;

public class TestThread {

    public static void main(String[] args) {
        try {
            new TestThread().myRun();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 线程调用interrupt()并不能终止线程,而是给线程做了一个标记,需要在任务里面加一个判断isInterrupted(),如果终止标记为true则结束线程
     */
    public static class MyThread extends Thread {
        @Override
        public void run() {
            while (!isInterrupted()) {
                System.out.println("任务执行: isInterrupted = " + isInterrupted());
            }

            System.out.println("任务结束: isInterrupted = " + isInterrupted());
        }
    }

    public void myRun() throws InterruptedException {
        Thread myThread = new MyThread();
        myThread.start();
        Thread.sleep(10);

        myThread.interrupt();
    }
}
